package com.men.tools;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

public class PatchGen {

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		boolean isSucess = true;
		
		String[] dirSrcs={"$$-error-$$"};//源文件路徑，支持多个
		String dirClassFrom="$$-error-$$";//class文件路徑
		String dirClassTo="$$-error-$$";//class文件路徑
		String dirBase="$$-error-$$";//要抽取的路径
		String dirPatch="patch";//版本存放路径
		String copySrc = "false";//是否同时拷贝源文件，默认不拷贝
		String toolPath = JarTool.getJarDir();//jar所在路径
		// 读取配置文件
		System.out.println("读取config.properties。。");
		String configPath="";
		InputStream is;
		try {
			configPath = toolPath+File.separator+"config.properties";
			is = new FileInputStream(new File(configPath));
			Properties pps = new Properties();
			pps.load(is);
			dirBase = pps.getProperty("dir_base", new File(toolPath).getParent());
			String dirSrc = pps.getProperty("dir_src", dirSrcs[0]);
			dirClassFrom = pps.getProperty("dir_class_from", dirClassFrom);
			dirClassTo = pps.getProperty("dir_class_to", dirClassFrom);//一定要在dirClassFrom赋值之后
			dirPatch = pps.getProperty("dir_patch", dirPatch);
			copySrc = pps.getProperty("copy_src", copySrc);
			//所有路径统一使用'/'
			dirBase = dirBase.replace("\\", "/");
			dirSrc = dirSrc.replace("\\", "/");
			dirClassFrom = dirClassFrom.replace("\\", "/");
			dirClassTo = dirClassTo.replace("\\", "/");
			dirPatch = dirPatch.replace("\\", "/");
			System.out.println("dir_base:"+dirBase);
			System.out.println("dir_src:"+dirSrc);
			System.out.println("dir_class_from:"+dirClassFrom);
			System.out.println("dir_class_to:"+dirClassTo);
			System.out.println("dir_patch:"+dirPatch);
			System.out.println("copy_src:"+copySrc);
			dirSrcs = dirSrc.split(",");//解析源文件夹设置
		} catch (FileNotFoundException e1) {
			System.out.println("没有找到config.properties,路径:"+configPath);
			e1.printStackTrace();
			isSucess = false;
		}catch (IOException e) {
			System.out.println("读取config.properties异常！");
			e.printStackTrace();
			isSucess = false;
		}
		System.out.println("=========================================================");
		System.out.println("");
		// 读取版本列表
		System.out.println("读取PatchFiles.txt。。");
		List<String> fileList = new ArrayList<String>();
		String filesPath="";
		try {
			filesPath = toolPath+File.separator+"PatchFiles.txt";
			InputStream is2 = new FileInputStream(new File(filesPath));
			InputStreamReader read = new InputStreamReader(is2);
			BufferedReader bufferedReader = new BufferedReader(read);
			String lineTxt = null;
			int i = 1;
			while ((lineTxt = bufferedReader.readLine()) != null) {
				lineTxt = lineTxt.trim();
				if(lineTxt.length()>0){
					lineTxt = lineTxt.replace("\\", "/");//所有路径统一使用'/'
					fileList.add(lineTxt);
					System.out.println(i+ " "+lineTxt);
					i++;
				}
			}
			read.close();
		} catch (FileNotFoundException e1) {
			System.out.println("没有找到PatchFiles.txt,路径:"+filesPath);
			e1.printStackTrace();
			isSucess = false;
		}catch (IOException e) {
			System.out.println("读取PatchFiles.txt异常！");
			e.printStackTrace();
			isSucess = false;
		}
		System.out.println("=========================================================");
		System.out.println("");
		//复制文件
		String patchPath = dirBase + File.separator + dirPatch + File.separator + "patch_"+new File(dirBase).getName()+"_"+DateUtil.getCurDate_yyyyMMdd();
		if(new File(patchPath).exists()){
			//版本路径已存在，退出
			System.out.println("版本路径已存在，请先转移或删除！");
			return ;
		}
		int j = 1;
		for(String f: fileList){
			String from =  f;
			String to =  f;
			//遍历源文件夹设置，看要复制的文件是否为源文件夹中的文件
			for(String s:dirSrcs){
				if(f.startsWith(s)){
					if("true".equals(copySrc)){
						//需要同时拷贝源文件
						try {
							System.out.println(j+" 同时拷贝源文件："+ dirBase + File.separator + from + "-->" + patchPath + File.separator + to);
							FileUtils.toCopy(dirBase + File.separator + from, patchPath + File.separator + to);
						} catch (IOException e) {
							System.out.println("复制文件异常！");
							e.printStackTrace();
							isSucess = false;
						}
					}
					//资源文件夹下的内容都要对应到class目录
					from = from.replaceFirst(s, dirClassFrom);
					//替换配置的源文件目标路径
					to = to.replaceFirst(s, dirClassTo);
					if(from.endsWith(".java")){
						//java文件，需要替换为class后缀
						from = from.replace(".java", ".class");
						to = to.replace(".java", ".class");
						//查看是否存在该类的内部类或匿名类
						File fromFile = new File(dirBase + File.separator + from);
						File[] brothers = fromFile.getParentFile().listFiles();//同目录下的所有文件
						for(File f2: brothers){
							if(f2.getName().startsWith(fromFile.getName().split("\\.")[0]+"$")){
								//以类名+“$”开头的为内部类，需要一块复制
								String from2 = from.replace(fromFile.getName(), f2.getName());
								String to2 = to.replace(new File(patchPath + File.separator + to).getName(), f2.getName());
								System.out.println(j+" 复制文件："+dirBase + File.separator + from2 + "-->" + patchPath + File.separator + to2);
								try {
									FileUtils.toCopy(dirBase + File.separator + from2, patchPath + File.separator + to2);
								} catch (IOException e) {
									System.out.println("复制文件异常！");
									e.printStackTrace();
									isSucess = false;
								}
							}
						}
						
					}
					break;
				}
			}
			System.out.println(j+" 复制文件："+dirBase + File.separator + from + "-->" + patchPath + File.separator + to);
			try {
				FileUtils.toCopy(dirBase + File.separator + from, patchPath + File.separator + to);
			} catch (IOException e) {
				System.out.println("复制文件异常！");
				e.printStackTrace();
				isSucess = false;
			}
			//经过上面的替换，to的java都替换为class了，除非没有设置源文件夹
			if(to.endsWith(".java")){
				//存在非源文件夹下的java文件，提示用户
				System.out.println("***************************************************");
				System.out.println("****警告****：存在非源文件夹下的java文件："+f);
				System.out.println("***************************************************");
			}
			j++;
		}
		System.out.println("=========================================================");
		System.out.println("");
		//将文件列表文件也拷贝过去
		try {
			FileUtils.toCopy( filesPath,  patchPath + File.separator + "PatchFiles.txt");
		} catch (IOException e) {
			System.out.println("复制文件异常！");
			e.printStackTrace();
			isSucess = false;
		}
		System.out.println("版本已生成，路径为："+patchPath);
		if(isSucess){
			System.out.println("");
			System.out.println("sucess！");
			System.out.println("");
		}else{
			System.out.println("");
			System.out.println("生成过程中发生异常！请注意查看！");
			System.out.println("");
		}
	}

}
